# coding=utf-8
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

from backstage.print import forms
from backstage.print.utils import view_funcs


class OrderList(APIView):

    authentication_classes = (SessionAuthentication,)
    permission_classes = (IsAdminUser,)

    def get(self, request):
        order_list = view_funcs.get_order_list()
        return Response({
            "status": 200,
            "msg": "success",
            "data": order_list,
        })


class OrderDetail(APIView):

    authentication_classes = (SessionAuthentication,)
    permission_classes = (IsAdminUser,)

    def delete(self, request, order_id):
        view_funcs.delete_order(order_id)
        return Response({
            "status": 200,
            "msg": "success",
        })

    def put(self, request, order_id):
        remark = request.data.get("remark")
        view_funcs.update_order(order_id, remark)
        return Response({
            "status": 200,
            "msg": "success",
        })

    def get(self, request, order_id):
        order_data = view_funcs.get_order_detail(order_id)
        if not order_data:
            return Response({
                "status": 404,
                "msg": "not found",
            })

        return Response({
            "status": 200,
            "msg": "success",
            "data": order_data,
        })
